package com.nineclock.attendance.utils;

import cn.hutool.core.date.DateUtil;
import com.nineclock.attendance.dto.AttendGroupDTO;
import com.nineclock.attendance.dto.AttendPunchDTO;
import com.nineclock.attendance.enums.AttendEnums;
import com.nineclock.attendance.pojo.AttendPunch;
import com.nineclock.common.utils.DateTimeUtil;
import com.nineclock.common.utils.DistanceUtil;

import java.util.Date;
import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class PunchUtils {
    /**
     * 设置打卡类型 : 1正常；2迟到；3早退；4旷工 5非工作日打卡
     * @param attendPunch
     * @param attendGroupDTO
     */
    public static void setPunchType(AttendPunch attendPunch, AttendGroupDTO attendGroupDTO) {
        //获取考勤组中的允许迟到分钟数
        Integer allowLateMinutes = attendGroupDTO.getAllowLateMinutes();

        //获取考勤组中的旷工迟到分钟数
        Integer lateMinutes = attendGroupDTO.getLateMinutes();

        //获取实际打卡时间字符串
        Date punchTime = attendPunch.getPunchTime();
        String punchTimeStr = DateTimeUtil.dateToStr(punchTime, DateTimeUtil.TIME_FORMAT_1);

        //默认正常
        AttendEnums punchType = AttendEnums.PUNCH_TYPE_OK;


        //判定
        //上午
        if(attendPunch.getPunchOnOffWork() == AttendEnums.NOON_TYPE_MORNING.value()){
            //获取关联的考勤组的设置上班时间
            String startWorkTime = attendGroupDTO.getStartWorkTime();

            //判定上班打卡时间
            if(DateTimeUtil.compareTime(punchTimeStr, startWorkTime, allowLateMinutes) == 1){ //返回1 , 说明 : punchTimeStr > startWorkTime + allowLateMinutes
                punchType = AttendEnums.PUNCH_TYPE_LATE;
                if(DateTimeUtil.compareTime(punchTimeStr, startWorkTime, lateMinutes) == 1){  //返回1 , 说明 : punchTimeStr > startWorkTime + lateMinutes
                    punchType = AttendEnums.PUNCH_TYPE_STAYALAY;
                }
            }

            attendPunch.setMorningPunchType(punchType.value());

            //下午
        }else{
            String offWorkTime = attendGroupDTO.getOffWorkTime();

            //判定下班打卡时间
            if(DateTimeUtil.compareTime(offWorkTime, punchTimeStr, allowLateMinutes) == 1){ //返回1 , 说明 : offWorkTime > punchTimeStr + allowLateMinutes
                punchType = AttendEnums.PUNCH_TYPE_EARLY;
                if(DateTimeUtil.compareTime(offWorkTime, punchTimeStr, lateMinutes) == 1){  //返回1 , 说明 : punchTimeStr > startWorkTime + lateMinutes
                    punchType = AttendEnums.PUNCH_TYPE_STAYALAY;
                }
            }

            attendPunch.setAfternoonPunchType(punchType.value());
        }

    }

    /**
     * 校验工作日
     * @param attendGroupDTO
     * @return
     */
    public static boolean validateWorkDay(AttendGroupDTO attendGroupDTO) {
        int week = DateUtil.thisDayOfWeek();   //1: 表示周日, 2: 表示周一, 3:表示周二 4:周三 5  周四 6 周五 7  周六   周日
                                               //            0           1        2     3      4      5       6
        List<String> workdays = attendGroupDTO.getWorkdays(); // [1,1,1,1,1,0,0]

        if(week == 1){
            week = 6;
        }else{
            week -= 2;
        }

        boolean flag = Integer.valueOf(workdays.get(week)) == 1;
        return flag;
    }

    /**
     * 校验打卡范围
     * @param attendPunchDTO
     * @param attendGroupDTO
     * @return
     */
    public static boolean validatePunchArea(AttendPunchDTO attendPunchDTO, AttendGroupDTO attendGroupDTO) {
        //比较"打卡坐标" 和 "考勤组打卡坐标"的距离
        Integer meter = DistanceUtil.getDistanceMeter(attendPunchDTO.getLat().doubleValue(), attendPunchDTO.getLng().doubleValue(), attendGroupDTO.getLat().doubleValue(), attendGroupDTO.getLng().doubleValue());
        //判定是否小于等于考勤组中设置的范围
        return meter <= attendGroupDTO.getAddressRange();
    }
}